package com.mysoft.gfyx.marketdevelop.service.impl;

import com.google.common.base.Joiner;
import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.modeling.dto.*;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.marketdevelop.controller.dto.GetDetailDataRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.BaseTjFilterDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.BaseTjListResponseDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetDeveloperFilterDataRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetDeveloperSortDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.ProjectListByDeveloperRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.SqlApiResponseDto;
import com.mysoft.gfyx.marketdevelop.service.DeveloperCommencePermitService;
import com.mysoft.gfyx.marketdevelop.service.DeveloperService;
import com.mysoft.gfyx.marketdevelop.service.DeveloperTakeLandService;
import com.mysoft.gfyx.marketdevelop.service.LoadDataApiCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DeveloperCommenceServiceImp extends BaseDataApiServiceImp implements DeveloperCommencePermitService {
    @Autowired
    private DeveloperService developerService;

    @Autowired
    private GetDeveloperDataService getDeveloperDataService;

    @Autowired
    private LoadDataApiCommonService loadDataApiCommonService;

    public FieldsSetting loadFields() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("uuid");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "developer_commence_permit";
        fieldsSetting.getFields().add(new DataApiField("uuid", groupName, "RowID", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("id", groupName, "主键id", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("commence_permit_id", groupName, "开工许可证ID", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("developer_name", groupName, "建设单位", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("sub_developer_name", groupName, "建设单位(子公司)", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("sub_organizing_code", groupName, "社会统一信用代码（子公司）", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("organizing_code", groupName, "社会统一信用代码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("construction_unit", groupName, "施工单位", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("publish_time", groupName, "签发日期", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("code", groupName, "开工许可证编码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("project_name", groupName, "项目名称", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contract_price", groupName, "合同金额", FieldType.Decimal));
        fieldsSetting.getFields().add(new DataApiField("start_time", groupName, "开工日期", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("completed_time", groupName, "竣工日期", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("province", groupName, "区域（省）", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("province_code", groupName, "区域（省）Code", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("city", groupName, "区域（市）", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("city_code", groupName, "区域（市）Code", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("prj_num", groupName, "项目编码", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadData(LoadDataParams options) {
        String creditCode = options.getOptions().getContext().get("id").toString();
        ProjectListByDeveloperRequestDto requestDto = new ProjectListByDeveloperRequestDto();
        requestDto.setOrganizing_code(creditCode);
        List<Filter> filters = options.getOptions().getFilters();
        requestDto = setFilter(requestDto, filters);
        requestDto.setPageNum(options.getPageIndex() + 1);
        requestDto.setPageSize(options.getPageSize());
        SqlApiResponseDto result = getDeveloperDataService.projectListByDeveloper(requestDto);
        return developerService.getSqlApiResFromLoadData(result);
    }

    public ProjectListByDeveloperRequestDto setFilter(ProjectListByDeveloperRequestDto requestDto, List<Filter> filters) {
        for (int i = 0; i < filters.size(); i++) {
            if (filters.get(i).getCondition().size() > 0) {
                String fieldName = filters.get(i).getCondition().get(0).getField();
                Object fieldValue = filters.get(i).getCondition().get(0).getValue();
                switch (fieldName) {
                    case "province":
                        if (!Objects.equals(fieldValue, "0")) {
                            requestDto.setProvince_code(fieldValue.toString());
                        }
                        break;
                    case "city_code":
                        requestDto.setCity_code(fieldValue.toString());
                        break;
                    case "organizing_code":
                        if (fieldValue instanceof ArrayList) {
                            String sub_code_collection = Joiner.on(",").join(((ArrayList<?>) fieldValue));
                            requestDto.setSub_organizing_code(sub_code_collection);
                        }
                        break;
                    case "project_name":
                        requestDto.setKeyword("%" + fieldValue.toString() + "%");
                        break;
                }
            }
        }
        return requestDto;
    }

    public void FilterProvince(List<Filter> filters) {
        if (filters.size() > 0) {
            for (int i = 0; i < filters.size(); i++) {
                if (filters.get(i).getCondition().size() > 0 && filters.get(i).getCondition().get(0).getField().equals("province_code") && filters.get(i).getCondition().get(0).getValue().equals("0")) {
                    filters.remove(i);
                }
            }
        }
    }

    public Map<String, Object> getCommencePermitDetail(GetDetailDataRequestDto dto) {
        //开工详情
        GetDeveloperFilterDataRequestDto requestDto = new GetDeveloperFilterDataRequestDto();
        requestDto = developerService.setCommonData(requestDto);
        requestDto.setDetail_table_name(DeveloperService.TABLE_COMMENCE_PERMIT);
        requestDto.setPage(1);
        requestDto.setPage_size(1);
        requestDto.addFilterDtos("id", BaseTjFilterDto.OPERATE_EQUAL, dto.getId(), DeveloperService.TABLE_COMMENCE_PERMIT, BaseTjFilterDto.ASSOCIATE_NONE);
        BaseTjListResponseDto res = getDeveloperDataService.getFilteredDeveloperDetailList(requestDto);
        if (res.getData().getList() == null) {
            throw new BusinessLogicException("未找到开工信息");
        }

        Map<String, Object> data = res.getData().getList().get(0).getItem();
        //建设单位
        Map<String, Object> companyData = developerService.getDeveloperMainData(data.get("organizing_code").toString());
        data.put("build_company", companyData);
        return data;
    }

}
